﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AlmaDatabase;
using Alma.Models;
using System.Diagnostics;

namespace Alma.Controllers
{
    public class ProductCriterionController : Controller
    {

        public ActionResult Create(int id)
        {
            ProductCriterionModel pcm = new ProductCriterionModel();
            pcm.ID_Product = id;
            using (var context = new AlmaDBEntities())
            {
                var criteria = from criterion in context.Criteria select criterion;
                List<string> list2 = new List<string>();
                foreach (Criterion c in criteria)
                {
                    list2.Add(c.Name);
                }
                ViewData["drop2"] = new SelectList(list2);
            }
            Debug.WriteLine("OLD_ID=" + pcm.ID_Product);
            return (View(pcm));
        }

        [HttpPost]
        public ActionResult Create(int id, ProductCriterionModel pcm)
        {
            ProductCriterion pc = new ProductCriterion();
            pc.ID_Product = id;
            using (var context = new AlmaDBEntities())
            {
                var product = from prods in context.Products
                              where prods.ID == id
                              select prods;
                var ID_Product = product.First().ID;

                var criterion = from prods in context.Criteria
                                where prods.Name == pcm.Name_Criterion
                                select prods;
                pc.ID_Criterion = criterion.First().ID;
                pc.Value = pcm.Value;
                context.ProductCriterions.AddObject(pc);
                context.SaveChanges();
            }
            return RedirectToAction("Edit", "Product", new { id = id });
        }

        public ActionResult Edit(int id, int idCriterion)
        {
            using (var context = new AlmaDBEntities())
            {
                var pc = from pcs in context.ProductCriterions
                         where pcs.ID_Criterion == idCriterion
                         where pcs.ID_Product == id
                         select pcs;
                ProductCriterionModel pcModel = new ProductCriterionModel(pc.First());
                /*drop product*/
                var prod = from prods in context.Products select prods;
                List<string> list = new List<string>();
                foreach (Product p in prod)
                {
                    list.Add(p.Name);
                }
                ViewData["drop"] = new SelectList(list);
                /*drop criterion*/
                var criteria = from criterion in context.Criteria select criterion;
                List<string> list2 = new List<string>();
                foreach (Criterion c in criteria)
                {
                    list.Add(c.Name);
                }
                ViewData["drop2"] = new SelectList(list);

                return View(pcModel);
            }
        }

        //
        // POST: /Section/Edit/5

        [HttpPost]
        public ActionResult Edit(ProductCriterionModel newSection)
        {
            if (!ModelState.IsValid)
            {
                return View(newSection);
            }
            long idProduct;
            using (var context = new AlmaDBEntities())
            {
                var product = from prods in context.Products
                              where prods.Name == newSection.Name_Product
                              select prods;
                var ID_Product = product.First().ID;
                idProduct = ID_Product;
                var criterion = from prods in context.Criteria
                                where prods.Name == newSection.Name_Criterion
                                select prods;
                var ID_Criterion = product.First().ID;

                var pc = from pcs in context.ProductCriterions
                         where pcs.ID_Criterion == ID_Criterion
                         where pcs.ID_Product == ID_Product
                         select pcs;
                pc.First().Value = newSection.Value;
                context.SaveChanges();
                /* var sections = from u in context.Sections
                                where u.ID == id
                                select u;
                 var editSection = sections.First();
                 editSection.Name = newSection.Name;
                 var cat = from cats in context.Categories
                           where cats.Name == newSection.Name_Category
                           select cats;
                 editSection.ID_Category = cat.First().ID;
                 editSection.Location = newSection.Location;
                 context.SaveChanges();*/
            }
            return RedirectToAction("Edit", "Product", new { id = idProduct });
        }

        public ActionResult Delete(int id, int idCriterion)
        {
            using (var context = new AlmaDBEntities())
            {
                var pc = from crits in context.ProductCriterions
                         where crits.ID_Criterion == idCriterion && crits.ID_Product == id
                         select crits;
                context.ProductCriterions.DeleteObject(pc.First());
                context.SaveChanges();
            }
            return RedirectToAction("Edit", "Product", new { id = id });
        }
    }
}
